package com.hxwm.hwdbtool.core;

import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;

import java.util.function.IntFunction;

public class CustomLineNumberFactory implements IntFunction<Node> {
    private final IntFunction<Node> base;
    private boolean showLineNumbers = true;
    private final CodeArea codeArea;

    public CustomLineNumberFactory(CodeArea codeArea) {
        this.base = LineNumberFactory.get(codeArea);
        this.codeArea = codeArea;
    }

    @Override
    public Node apply(int lineNumber) {
        if (!showLineNumbers) {
            return new StackPane(); // 返回空白区域
        }
        lineNumber ++;
        Label lineNo = new Label(String.format("%4d", lineNumber));
        lineNo.setFont(Font.font("monospace", 12)); // 设置字体和大小
        lineNo.setTextFill(Color.GRAY); // 设置行号颜色

        StackPane stack = new StackPane(lineNo);
        stack.setPadding(new Insets(0, 10, 0, 0)); // 设置右侧的填充以增加间距

        // 添加右键菜单
//        stack.setOnContextMenuRequested(event -> {
//            ContextMenu contextMenu = new ContextMenu();
//            MenuItem toggleItem = new MenuItem(showLineNumbers ? "隐藏行号" : "显示行号");
//            toggleItem.setOnAction(e -> {
//                showLineNumbers = !showLineNumbers;
//                codeArea.setParagraphGraphicFactory(this);
//            });
//            contextMenu.getItems().add(toggleItem);
//            contextMenu.show(stack, event.getScreenX(), event.getScreenY());
//        });

        return stack;
    }
}
